# 清理失效实例
import requests

def deregister_failed_instances(consul_url):
    response = requests.get(consul_url)
    if response.status_code == 200:
        instances = response.json()
        for instance in instances:
            service_id = instance['ServiceID']
            if service_id:
                requests.put(f"http://consul.opsx.top/v1/agent/service/deregister/{service_id}")
                print(f"失效实例ID: {service_id}")
    else:
        print(f"无法从 Consul API 获取数据。状态码：{response.status_code}")

consul_url = "http://consul.opsx.top/v1/health/state/any"
deregister_failed_instances(consul_url)





# curl -X PUT -d '{
# "id": "host-122",
# "name": "node-exporter", 
# "address": "192.168.0.120",
# "port": 9100,
# "tags": ["linux"], 
# "meta": { 
#    "group": "kong", 
#    "environment": "Pro", 
#    "project": "API_Platform"
#  },
# "checks": [ {
#     "http": "http://192.168.0.120:9100/metrics",
#     "interval": "5s"
#   }]}' \
# http://127.0.0.1:8500/v1/agent/service/register